//
//  ZYLFindBanners.m
//  Cartoon
//
//  Created by dllo on 15/7/16.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLFindBanners.h"
#import "UIImageView+WebCache.h"
#import "ZYLBannerModel.h"
@interface ZYLFindBanners ()
@property (nonatomic, retain) NSArray *arr;
@property (nonatomic, retain) UIImageView *img1;
@property (nonatomic, retain) UIImageView *img2;
@property (nonatomic, retain) UIImageView *img3;
@property (nonatomic, assign) NSInteger myCount;

@end

@implementation ZYLFindBanners


- (void)dealloc
{
    [_arr release];
    [_img1 release];
    [_img2 release];
    [_img3 release];
    [_page release];
    [super dealloc];
    
}

- (instancetype)initWithFrame:(CGRect)frame arr:(NSMutableArray *)arr
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createScrollWithFrame:frame arr:arr];
    }
    return self;
}

- (void)createScrollWithFrame:(CGRect)frame arr:(NSMutableArray *)arr
{
    self.arr = arr;
    self.contentSize = CGSizeMake(frame.size.width * 3, frame.size.height);
    self.contentOffset = CGPointMake(375, 0);
    self.pagingEnabled = YES;
    self.delegate = self;
    self.showsHorizontalScrollIndicator = NO;
    self.img1 = [[[UIImageView alloc] init] autorelease];
    [self addSubview:self.img1];
    self.img2 = [[[UIImageView alloc] init] autorelease];
    [self addSubview:self.img2];
    self.img3 = [[[UIImageView alloc] init] autorelease];
    [self addSubview:self.img3];
    self.img1.frame = CGRectMake(0, 0, 375, self.frame.size.height);
    self.img2.frame = CGRectMake(375, 0, 375, self.frame.size.height);
    self.img3.frame = CGRectMake(375 * 2, 0, 375, self.frame.size.height);
    [self.img1 sd_setImageWithURL:[NSURL URLWithString:[self.arr.lastObject pic]]];
    [self.img2 sd_setImageWithURL:[NSURL URLWithString:[self.arr.firstObject pic]]];
    [self.img3 sd_setImageWithURL:[NSURL URLWithString:[self.arr[1] pic]]];
    self.img2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    // 点击次数
    tap.numberOfTapsRequired = 1;
    // 几个手指
    tap.numberOfTouchesRequired = 1;
    // 把手势 添加在视图上
    [self.img2 addGestureRecognizer:tap];
    [tap release];
    self.myCount = 0;
    self.page = [[[UIPageControl alloc] init] autorelease];
    self.page.frame = CGRectMake(0, frame.size.height - 20, 375, 20);
    self.page.numberOfPages = self.arr.count;
    self.page.currentPage = 0;
    self.page.pageIndicatorTintColor = [UIColor lightGrayColor];
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoChange) userInfo:nil repeats:YES];
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"%@", [self.arr[self.myCount] pic]);
}


- (void)autoChange
{
    [self setContentOffset:CGPointMake(375 * 2, 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 只要视图滚动就会触发的方法
    // 任何偏移量的变化 就会触发这个方法
    if (scrollView.contentOffset.x == 0) {
        if (self.myCount == 0) {
            self.myCount = self.arr.count - 1;
            [self.img1 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount - 1] pic]]];
            [self.img2 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount] pic]]];
            [self.img3 sd_setImageWithURL:[NSURL URLWithString:[self.arr[0] pic]]];
        } else if (self.myCount == 1) {
            self.myCount -= 1;
            [self.img1 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.arr.count - 1] pic]]];
            [self.img2 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount] pic]]];
            [self.img3 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount + 1] pic]]];
        } else {
            self.myCount -= 1;
            [self.img1 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount - 1] pic]]];
            [self.img2 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount] pic]]];
            [self.img3 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount + 1] pic]]];
        }
        scrollView.contentOffset = CGPointMake(375, 0);
    }
    if (scrollView.contentOffset.x == 375 * 2) {
        if (self.myCount == self.arr.count - 1) {
            self.myCount = 0;
            [self.img3 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount + 1] pic]]];
            [self.img1 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.arr.count - 1] pic]]];
            [self.img2 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount] pic]]];
        } else if (self.myCount == self.arr.count - 2) {
            self.myCount += 1;
            [self.img3 sd_setImageWithURL:[NSURL URLWithString:[self.arr[0] pic]]];
            [self.img1 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount - 1] pic]]];
            [self.img2 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount] pic]]];
        }else {
            self.myCount += 1;
            [self.img1 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount - 1] pic]]];
            [self.img2 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount] pic]]];
            [self.img3 sd_setImageWithURL:[NSURL URLWithString:[self.arr[self.myCount + 1] pic]]];
        }
        scrollView.contentOffset = CGPointMake(375, 0);
    }
    self.page.currentPage = self.myCount;
}


@end
